package com.yueman.ptoolopenticket.ticket.dto;

import cn.hutool.core.util.ObjectUtil;
import com.yueman.ptoolopenticket.ticket.utils.Constant;

public class ResultBuilder {
    public static <T> Result success(T body) {
        return new ResultBuilderBuilder<T>().responseResult(ResponseResult.SUCCESS).result(ObjectUtil.isEmpty(body)?(T) "{}" : body).build();
    }
    public static <T> Result fail(ResponseResult responseResult) {
        return new ResultBuilderBuilder<T>().responseResult(responseResult).result((T) "{}").build();
    }
   public static <T> Result fail(Object o) {
        return new ResultBuilderBuilder<T>().responseResult(o).result((T) "{}").build();
    }
    public static class ResultBuilderBuilder<T> {
        private Result result;

        ResultBuilderBuilder() {
            this.result = new Result();
        }

        private ResultBuilderBuilder<T> responseResult(ResponseResult responseResult) {
             this.result.setRetCode(responseResult.states);
             this.result.setRetMsg(responseResult.retmsg);
             return this;
        }
       private ResultBuilderBuilder<T> responseResult(Object o) {
           this.result.setRetCode(Constant.FAIL_CODE);
           this.result.setRetMsg(o.toString());
           return this;
       }
        private Result build() {
            return this.result;
        }

        public ResultBuilderBuilder<T> result(T t) {
             this.result.setBody(t);
             return this;
        }
    }
}
